package com.hbnu.test.www.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpReceive {
    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;
        try {
            datagramSocket = new DatagramSocket(8888);
            byte[] bytes = new byte[1024];
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
            datagramSocket.receive(datagramPacket);
            String ip = datagramPacket.getAddress().getHostAddress();
            int port = datagramPacket.getPort();
            byte[] data = datagramPacket.getData();
            String result = new String(data,0,datagramPacket.getLength());
            System.out.println(ip + ":" + port + "-->" + result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            if(datagramSocket!=null){
                datagramSocket.close();
            }
        }
    }
}
